package com.wn55.hrms.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Dept;
import com.wn55.hrms.mapper.DeptMapper;
import com.wn55.hrms.service.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 曹金鑫~~~
 * @date 2022/04/29/1:34
 */
@Service
@CacheConfig(cacheNames = {"{dept}"})
public class DeptService {

    @Autowired
    DeptMapper deptMapper;


    /**
     * 部门查询
     *
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @Cacheable(value = "dept",keyGenerator = "keyGenerator")
    public PageInfo<DeptVo> selectDeptByCondition(Integer currentPage, Integer pageSize, String name) {
        PageHelper.startPage(currentPage, pageSize);
        List<DeptVo> list = deptMapper.selectDeptByCondition(name);
        return new PageInfo<>(list);
    }

    /**
     * 部门添加
     *
     * @param dept
     */
    @CachePut(value = "dept",keyGenerator = "keyGenerator")
    public void addDept(Dept dept) {
        deptMapper.insert(dept);
    }

    /**
     * 部门修改
     *
     * @param dept
     */
    @CachePut(value = "dept",keyGenerator = "keyGenerator")
    @Transactional(rollbackFor = Exception.class)
    public void editDept(Dept dept) {
        deptMapper.updateByPrimaryKey(dept);
    }

    /**
     * 删除部门
     *
     * @param id
     */
    @CacheEvict(value = "dept",allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void deleteDeptById(int id) {
        deptMapper.updateDeptById(id);
    }

    /**
     * 获取部门
     */
    public List<Dept> initDept() {
        List<Dept> depts = deptMapper.selectDeptlist();
        return depts;
    }
}
